--- ========================================================= ---
---  Setup Revision, Version, Date and add to AceLibrary
--- ========================================================= ---
local VERSION = tonumber(("$Revision: 52619 $"):match("%d+"))

local DaHud = DaHud
DaHud_Module_Buffs = DaHud:NewModule("Module_Buffs", "AceEvent-2.0")
local self = DaHud_Buffs
if DaHud.revision < VERSION then
	DaHud.version = "r" .. VERSION
	DaHud.revision = VERSION
	DaHud.date = ("$Date: 2007-10-19 21:07:04 -0400 (Fri, 19 Oct 2007) $"):match("%d%d%d%d%-%d%d%-%d%d")
end
DaHud.desc = "Buffs Module"

--- ========================================================= ---
---  Setup Locals
--- ========================================================= ---
local L = DaHud:L("DaHud_Module_Buffs")

--- ========================================================= ---
---  On Intitalize / Setup Database
--- ========================================================= ---
function DaHud_Module_Buffs:OnInitialize()
	self.db = DaHud:AcquireDBNamespace("Module_Buffs")
	DaHud:RegisterDefaults("Module_Buffs", "profile", {
		number = 2,
		hudbuffs1 = {"Buffs", true, "LOW", 4, 20, 20, 1, "left", -45, -120, 5, "target", "buff", 20, 2, "always"},
		hudbuffs2 = {"Debuffs", true, "LOW", 4, 20, 20, 1, "right", 45, -120, 5, "target", "debuff", 20, 2, "always"},
	})
end

--- ========================================================= ---
---  On Enable
--- ========================================================= ---
function DaHud_Module_Buffs:OnEnable()
	self:SetupOptions()
	self:CreateFrames()
	self:UpdateAllFrames()
	
	self:RegisterEvent("DaHud_SetConfigMode")
	self:RegisterEvent("DaHud_ReloadAllFrames")
end

--- ========================================================= ---
---  On Disable
--- ========================================================= ---
function DaHud_Module_Buffs:OnDisable()
	self:UnregisterAllEvents();
end

--- ========================================================= ---
---  DaHud_SetConfigMode
--- ========================================================= ---
function DaHud_Module_Buffs:DaHud_SetConfigMode()
	self:UpdateAllFrames()
end

--- ========================================================= ---
---  DaHud_ReloadAllFrames
--- ========================================================= ---
function DaHud_Module_Buffs:DaHud_ReloadAllFrames()
	self:SetupOptions()
	self:UpdateAllFrames()
end

--- ========================================================= ---
---  Setup Validates for Options
--- ========================================================= ---
local strataValidate = {
	BACKGROUND = L["Background"],
	LOW = L["Low"],
	MEDIUM = L["Medium"],
	HIGH = L["High"],
	DIALOG = L["Dialog"],
	TOOLTIP = L["Tooltip"],
}

local strataValidateDesc = {}

local eventUnitValidate = {
	player = L["Player"],
	target = L["Target"],
	focus = L["Focus"],
	pet = L["Pet"],
	targettarget = L["Target of Target"],
	targettargettarget = L["ToT of Target"],
}

local eventUnitValidateDesc = {}

local tooltipValidate = {
	always = L["Always"],
	never = L["Never"],
	ooc = L["Out of Combat"],
}

local tooltipValidateDesc = {}

local alignValidate = {
	left = L["Left"],
	right = L["Right"],
}

local alignValidateDesc = {}

--- ========================================================= ---
---  Setup Options
--- ========================================================= ---
function DaHud_Module_Buffs:SetupOptions()
	DaHud.options.args.buffsframes = {
		name = L["Buffs Frames"],
		desc = L["Change the Buffs Frame options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.number do
		local frame = "hudbuffs"..i
			
		self:UpdateMainOption(frame)
		self:UpdateNameOption(frame)
		self:UpdateShowOption(frame)
		self:UpdateFramestrataOption(frame)
		self:UpdateFramelevelOption(frame)
		self:UpdateWidthOption(frame)
		self:UpdateHeightOption(frame)
		self:UpdateScaleOption(frame)
		self:UpdateAlignOption(frame)
		self:UpdatePosXOption(frame)
		self:UpdatePosYOption(frame)
		self:UpdateSpacingOption(frame)
		self:UpdateUnitOption(frame)
		self:UpdateNumberOption(frame)
		self:UpdateRowsOption(frame)
		self:UpdateTooltipOption(frame)
	end
end

--- ========================================================= ---
---  Update Main Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateMainOption(frame)
	local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])

	DaHud.options.args.buffsframes.args[frame] = {
		name = name,
		desc = L["Change the Buffs Frame options."],
		type = 'group',
		args = {
		}
	}
end

--- ========================================================= ---
---  Update Name Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateNameOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.name = {
		name = L["Name"],
		desc = L["Name the Frame."],
		type = 'text',
		usage = "<name>",
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return name
		end,
		set = function(value) 
			self:SaveOptions(frame, "name", value)
			self:SetupOptions()
		end,
	}
end

--- ========================================================= ---
---  Update Show Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateShowOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.show = {
		name = L["Show"],
		desc = L["Show this Frame."],
		type = 'toggle',
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return show
		end,
		set = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			self:SaveOptions(frame, "show", not(show))
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Framestrata Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateFramestrataOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.framestrata = {
		name = L["Frame Strata"],
		desc = L["Choose the layer the buffs is drawn on."],
		type = 'text',
		validate = strataValidate,
		validateDesc = strataValidateDesc,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return framestrata
		end,
		set = function(value) 
			self:SaveOptions(frame, "framestrata", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Framelevel Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateFramelevelOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.framelevel = {
		name = L["Frame Level"],
		desc = L["Change the Frame Level it is drawn on."],
		type = 'range',
		min = 1,
		max = 12,
		step = 1,
		bigStep = 2,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return framelevel
		end,
		set = function(value) 
			self:SaveOptions(frame, "framelevel", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Width Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateWidthOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.width = {
		name = L["Width"],
		desc = L["Change the Width."],
		type = 'range',
		min = 0,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return width
		end,
		set = function(value) 
			self:SaveOptions(frame, "width", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Height Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateHeightOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.height = {
		name = L["Height"],
		desc = L["Change the Height."],
		type = 'range',
		min = 0,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return height
		end,
		set = function(value) 
			self:SaveOptions(frame, "height", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Scale Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateScaleOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.scale = {
		name = L["Scale"],
		desc = L["Change the Scale."],
		type = 'range',
		min = 0.25,
		max = 3,
		step = 0.01,
		bigStep = 0.05,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return scale
		end,
		set = function(value) 
			self:SaveOptions(frame, "scale", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Align Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateAlignOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.align = {
		name = L["Buff Align"],
		desc = L["Choose which side to Align the Buffs in the Frame."],
		type = 'text',
		validate = alignValidate,
		validateDesc = alignValidateDesc,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return align
		end,
		set = function(value) 
			self:SaveOptions(frame, "align", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update POS X Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdatePosXOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.posx = {
		name = L["Pos X"],
		desc = L["Change the X Position."],
		type = 'range',
		min = -500,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return posx
		end,
		set = function(value) 
			self:SaveOptions(frame, "posx", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update POS Y Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdatePosYOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.posy = {
		name = L["Pos Y"],
		desc = L["Change the Y Position."],
		type = 'range',
		min = -500,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return posy
		end,
		set = function(value) 
			self:SaveOptions(frame, "posy", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Spacing Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateSpacingOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.spacing = {
		name = L["Spacing"],
		desc = L["Change the Spacing of the Buttons."],
		type = 'range',
		min = 0,
		max = 20,
		step = 1,
		bigStep = 2,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return spacing
		end,
		set = function(value) 
			self:SaveOptions(frame, "spacing", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Unit Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateUnitOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.unit = {
		name = L["Event Unit"],
		desc = L["Choose which Unit the Event is monitoring and showing."],
		type = 'text',
		validate = eventUnitValidate,
		validateDesc = eventUnitValidateDesc,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return unit
		end,
		set = function(value) 
			self:SaveOptions(frame, "unit", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Number Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateNumberOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.number = {
		name = L["Number of Buffs/Debuffs"],
		desc = L["Change the Number of Buffs/Debuffs to Show."],
		type = 'range',
		min = 0,
		max = 40,
		step = 1,
		bigStep = 5,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return number
		end,
		set = function(value) 
			self:SaveOptions(frame, "number", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Rows Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateRowsOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.rows = {
		name = L["Number of Rows"],
		desc = L["Change the Number of Rows."],
		type = 'range',
		min = 1,
		max = 5,
		step = 1,
		bigStep = 2,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return rows
		end,
		set = function(value) 
			self:SaveOptions(frame, "rows", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Tooltip Option
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateTooltipOption(frame)
	DaHud.options.args.buffsframes.args[frame].args.tooltip = {
		name = L["Tooltip"],
		desc = L["Choose when to show the Tooltip (Only works if Button is Enabled)."],
		type = 'text',
		validate = tooltipValidate,
		validateDesc = tooltipValidateDesc,
		get = function()
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
			return tooltip
		end,
		set = function(value) 
			self:SaveOptions(frame, "tooltip", value)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Save Options
--- ========================================================= ---
function DaHud_Module_Buffs:SaveOptions(frame, var, value)
	local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[frame])
	
	if var == "name" then
		name = value
	elseif var == "show" then
		show = value
	elseif var == "framestrata" then
		framestrata = value
	elseif var == "framelevel" then
		framelevel = value
	elseif var == "width" then
		width = value
	elseif var == "height" then
		height = value
	elseif var == "scale" then
		scale = value
	elseif var == "align" then
		align = value
	elseif var == "posx" then
		posx = value
	elseif var == "posy" then
		posy = value
	elseif var == "spacing" then
		spacing = value
	elseif var == "unit" then
		unit = value
	elseif var == "number" then
		number = value
	elseif var == "rows" then
		rows = value
	elseif var == "tooltip" then
		tooltip = value
	end
	
	self.db.profile[frame] = {name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip}
end	

--- ========================================================= ---
---  Create Frame
--- ========================================================= ---
function DaHud_Module_Buffs:CreateFrame(frametype, framename, parent, vals)
	local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[vals])
	local f = CreateFrame(frametype, framename, parent)

	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("Center", parent, "Center", posx, posy)
	f:SetFrameStrata(framestrata)
	f:SetFrameLevel(framelevel)
	
	f.unit = unit
	f.framename = framename
	
	f:SetAttribute("unit", unit)
	f:SetAttribute("shown", "unit")
	f:SetAttribute("useunit", unit)
	RegisterUnitWatch(f)
	
	f:SetScript("OnEvent", function(self)

	end)

	f:Show()

	return f
end

--- ========================================================= ---
---  Create Buffs Button
--- ========================================================= ---
function DaHud_Module_Buffs:CreateButton(type, framename, parent, id, bufftype, vals, valx, valy)
	local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[vals])
	local f = CreateFrame(type, framename, parent)

	f.framename = framename
	f.tt = tooltip
	f.unit = unit
	f.bufftype = bufftype
	f.number = number

	f:SetID(id)
	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("Center", parent, "Center", posx+valx, posy+valy)
	f:SetFrameLevel(framelevel+1)

	f:RegisterForClicks("AnyUp")
	
	f:SetScript("OnEnter", function(self)
		if self.tt == "always" or (self.tt == "ooc" and not(InCombatLockdown())) then
			if (not this:IsVisible()) then return end
			GameTooltip:SetOwner(this, "ANCHOR_BOTTOMRIGHT")
			local unit = this.unit
			if (self.bufftype == "buff") then
				GameTooltip:SetUnitBuff(unit, self:GetID())
			elseif (self.bufftype == "debuff") then
				GameTooltip:SetUnitDebuff(unit, self:GetID())
			end
			GameTooltip:Show()
		end
	end)
		
	f:SetScript("OnLeave", function(self) 
		if(GameTooltip:IsOwned(self)) then
			GameTooltip:Hide()
		end
	end)
	
	f:SetScript("OnEvent", function(self)
		if DaHud_ConfigMode then return end
		
		if self:GetID() > number then
			f:Hide()
			return 
		end
	
		if (self.bufftype == "buff") then
			local buffName, rank, iconTexture, count, duration, timeLeft = UnitBuff(self.unit, self:GetID())
			local f = getglobal(self.framename)
			if buffName then
				f.Icon:SetTexture(iconTexture)
				if count and count > 1 then
					f.Count:SetText(count)
					f.Count:Show()
				else
					f.Count:SetText("")
					f.Count:Hide()
				end
				f:Show()
			else
				f:Hide()
			end
		elseif (self.bufftype == "debuff") then
			local buffName, rank, iconTexture, count, debuffType, duration, timeLeft = UnitDebuff(self.unit, self:GetID())
			local color
			local f = getglobal(self.framename)
			if buffName then
				f.Icon:SetTexture(iconTexture)
				if count and count > 1 then
					f.Count:SetText(count)
					f.Count:Show()
				else
					f.Count:SetText("")
					f.Count:Hide()
				end
				if debuffType then
					color = DebuffTypeColor[debuffType]
				else
					color = DebuffTypeColor["none"]
				end
				f.Border:SetVertexColor(color.r, color.g, color.b)
				f.Border:Show()
				f:Show()
			else
				f.Border:Hide()
				f:Hide()
			end
		end
	end)

	f:RegisterEvent("UNIT_AURA")
	f:RegisterEvent("PLAYER_TARGET_CHANGED")
	f:RegisterEvent("PLAYER_FOCUS_CHANGED")
	
	f.Icon = f:CreateTexture(framename.."Texture", "BACKGROUND")
	f.Icon:SetTexture("Interface\\Icons\\INV_Misc_Ear_Human_02")
	f.Icon:SetAllPoints(true)
	f.Icon:Show()
	
	f.Border = f:CreateTexture(framename.."Border", "BORDER")
	f.Border:SetTexture("Interface\\Buttons\\UI-Debuff-Overlays")
	f.Border:SetTexCoord(0.296875, 0.5703125, 0, 0.515625)
	f.Border:SetWidth(width+1)
	f.Border:SetHeight(height+1)
	f.Border:SetPoint("CENTER", f, "CENTER")
	f.Border:Hide()
	
	f.Count = f:CreateFontString(framename.."Count", "OVERLAY", "NumberFontNormalSmall")
	f.Count:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 0)
	f.Count:Show()
	
	f:Show()
end

--- ========================================================= ---
---  Create Frames
--- ========================================================= ---
function DaHud_Module_Buffs:CreateFrames()
	for i=1, self.db.profile.number do
		self:CreateFrame("Frame", "DaHudBuffs"..i, getglobal("DaHudFrame"), "hudbuffs"..i)
		for b=1, 40 do
			local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile["hudbuffs"..i])
			local xgap = 0
			local ygap = 0
			if align == "left" then
				if b == 1 then
					xgap = 0
				else
					xgap = -(width+spacing)*(b-1)
				end
			elseif align == "right" then
				if b == 1 then
					xgap = 0
				else
					xgap = (width+spacing)*(b-1)
				end
			end
			self:CreateButton("Button", "DaHudBuffs"..i.."_"..bufftype.."Button"..b, getglobal("DaHudBuffs"..i), b, bufftype, "hudbuffs"..i, xgap, ygap)
		end
	end
end

--- ========================================================= ---
---  Update All Frames
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateAllFrames()
	for i=1, self.db.profile.number do
		self:UpdateFrame("DaHudBuffs"..i, "hudbuffs"..i)
		local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile["hudbuffs"..i])
		local xgap, ygap, row, column = 0, 0, 0, 0
		local split = ceil(number/rows)
		for b=1, 40 do
			if (column+1) <= split then
				column = column + 1
			else
				column = 1
			end
			if column == 1 then
				row = row + 1
			end
			if align == "left" then
				xgap = -(width+spacing)*(column-1)
			elseif align == "right" then
				xgap = (width+spacing)*(column-1)
			end
			ygap = -(height+spacing)*(row-1)
			self:UpdateButton("DaHudBuffs"..i, "DaHudBuffs"..i.."_"..bufftype.."Button"..b, "hudbuffs"..i, xgap, ygap)
		end
	end
end

--- ========================================================= ---
---  Update Frame
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateFrame(framename, vals)
	local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[vals])
	local f = getglobal(framename)
	
	if DaHud_ConfigMode then
		unit = "player"
	end
	
	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("Center", getglobal("DaHudFrame"), "Center", posx, posy)
	f:SetFrameStrata(framestrata)
	f:SetFrameLevel(framelevel)
	
	f.unit = unit
	f.framename = framename
	
	f:SetAttribute("unit", unit)
	f:SetAttribute("shown", "unit")
	f:SetAttribute("useunit", unit)
	
	if show then
		RegisterUnitWatch(f)
	else
		UnregisterUnitWatch(f)
		f:Hide()
	end	
end

--- ========================================================= ---
---  Update Button
--- ========================================================= ---
function DaHud_Module_Buffs:UpdateButton(anchor, framename, vals, valx, valy)
	local name, show, framestrata, framelevel, width, height, scale, align, posx, posy, spacing, unit, bufftype, number, rows, tooltip = unpack(self.db.profile[vals])
	local f = getglobal(framename)

	if DaHud_ConfigMode then
		unit = "player"
	end
	
	f.framename = framename
	f.tt = tooltip
	f.unit = unit
	f.bufftype = bufftype
	f.number = number
	
	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("Center", getglobal(anchor), "Center", posx+valx, posy+valy)
	f:SetFrameLevel(framelevel+1)
	
	if f:GetID() > number then
		f:Hide()
		return 
	end
	
	if DaHud_ConfigMode then
		f.Icon:SetTexture(iconTexture)
	end
	
	if (bufftype == "buff") then
		local buffName, rank, iconTexture, count, duration, timeLeft = UnitBuff(unit, f:GetID())
		local f = getglobal(framename)
		if buffName then
			f.Icon:SetTexture(iconTexture)
			if count and count > 1 then
				f.Count:SetText(count)
				f.Count:Show()
			else
				f.Count:SetText("")
				f.Count:Hide()
			end
			f:Show()
		else
			if DaHud_ConfigMode then
				f.Icon:SetTexture("Interface\\Icons\\INV_Misc_Ear_Human_02")
				f:Show()
			else
				f:Hide()
			end
		end
	elseif (bufftype == "debuff") then
		local buffName, rank, iconTexture, count, debuffType, duration, timeLeft = UnitDebuff(unit, f:GetID())
		local color
		local f = getglobal(framename)
		if buffName then
			f.Icon:SetTexture(iconTexture)
			if count and count > 1 then
				f.Count:SetText(count)
				f.Count:Show()
			else
				f.Count:SetText("")
				f.Count:Hide()
			end
			if debuffType then
				color = DebuffTypeColor[debuffType]
			else
				color = DebuffTypeColor["none"]
			end
			f.Border:SetVertexColor(color.r, color.g, color.b)
			f.Border:Show()
			f:Show()
		else
			if DaHud_ConfigMode then
				f.Icon:SetTexture("Interface\\Icons\\INV_Misc_Ear_Human_02")
				f:Show()
			else
				f.Border:Hide()
				f:Hide()
			end
		end
	end
end